package com.qili.controller.sys;

import com.qili.entity.supervisor.SupervisorFile;
import com.qili.service.supervisor.SupervisorFileService;
import com.qili.util.IpUtil;
import com.qili.util.UploadUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @author liuyang
 * @date 2021/2/27
 * @description pdf操作
 */
@Controller
@RequestMapping("/pdf")
public class PdfController {

    @Autowired
    private SupervisorFileService supervisorFileService;
    @Autowired
    private UploadUtil uploadUtil;

    /**
     * 根据file表主键,获取pdf文件的访问路径
     * 适用于人员资质库文件     /userfile指向的目录
     * @param fileId    supervisor_file表主键
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/open")
    public String openPdf(String fileId, Model model, HttpServletRequest request){
        if(StringUtils.isNotBlank(fileId)){
            SupervisorFile supervisorFile = supervisorFileService.selectByPrimaryKey(fileId);
            StringBuilder filePath = new StringBuilder();
            String uri = IpUtil.getUri(request);
            filePath = filePath.append(uri)
                    .append(File.separator)
                    .append("userfile")
                    .append(supervisorFile.getUrl().substring((uploadUtil.getUploadPath() + uploadUtil.getUserPath()).length()));
            model.addAttribute("filePath",filePath.toString());
            return "/office/viewPdf";
        }
        return "error";
    }

}
